<?php

namespace App\Api;

use PhalApi\Api;
use PhalApi\Exception\BadRequestException;
use PhpOffice\PhpWord\TemplateProcessor;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

use Qiniu\Auth;


/**
 * 钉钉通知
 *   @desc 钉钉通知
 */
class Notice extends Api
{

    public function getRules()
    {
        return array(
            'dingding' => array(
                "content" => array('name' => 'content', 'type' => 'string', "default" => 'gitcode', 'require' => false, 'desc' => '通知内容'),
            ),
        );
    }

    public function sendText($data)
    {
        $ret = "";
        foreach ($data as $key => $value) {
            if( $value ){
                $ret .= $key . ":" . $value . "\n";
            }
        }
        return $ret;
    }


    /**
     * 钉钉发送消息
     * @desc 添加参数
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function dingding()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

      
        $token = "c230bf201cf694db851c14413af45f3c216754466886f7f46647eb3678d809a4";

        $date_time = date("Y-m-d H:i:s",time());
        $this->ding($token, $this->content == 'gitcode' ? $date_time : $this->content);
        return $date_time;
    }

    /**
     * 钉钉推送
     */

    public function ding($token, $msg){
        //这里是钉钉提示的开始
        $curl = new \PhalApi\CUrl();
        // 设置请求的头部信息。
        $curl->setHeader(array('Content-Type' => 'application/json'));
        // 设置curl选项
        $postDate = array(
            "msgtype" => "text",
            "text" => array(
                "content" => "通知: " . $msg
            )
        );
        $url = "https://oapi.dingtalk.com/robot/send?access_token=$token";
        $curl->post($url, json_encode($postDate),2000);
    }


}
